import groovy.sql.Sql
import spock.lang.Shared
import spock.lang.Specification

/**
 * Created by houjinxin on 17-3-31.
 */
class DatabaseDrivenSpec extends Specification{

    @Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver")

    @Shared sql1 = Sql.newInstance("jdbc:mysql://127.0.0.1:3306/crawler_new", "root", "root", "com.mysql.jdbc.Driver")

    // insert data (usually the database would already contain the data)
    def setupSpec() {
        sql.execute("create table maxdata (id int primary key, a int, b int, c int)")
        sql.execute("insert into maxdata values (1, 3, 7, 7), (2, 5, 4, 5), (3, 9, 9, 9)")
    }


    def "maximum of two numbers"() {

        expect:
            Math.max(a, b) == c

        where:
            [a, b, c] << sql.rows("select a, b, c from maxdata")
    }

    def "get data from mysql"() {

        expect:
            println "$a $b $c"

        where:
            [a, b, c] << sql1.rows("select operator, province, source from t_yys_route_log")
    }

    def "comparing two decimal numbers"() {
        def myPi = 3.14

        expect:
            myPi closeTo(Math.PI, 0.01)
    }

}
